package com.zy.tbshoppingcart;import android.app.Activity;import android.content.Context;import android.content.DialogInterface;import android.os.Bundle;import android.support.v7.app.AlertDialog;import android.view.View;import android.view.Window;import android.widget.CheckBox;import android.widget.ExpandableListView;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;import com.zy.tbshoppingcart.adapter.ShopcartAdapter;import com.zy.tbshoppingcart.entity.GoodsInfo;import com.zy.tbshoppingcart.entity.StoreInfo;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Random;import butterknife.BindView;import butterknife.ButterKnife;import butterknife.OnClick;/** * 模仿淘宝购物车界面 */public class ShopcartActivity extends Activity implements ShopcartAdapter.CheckInterface, ShopcartAdapter.ModifyCountInterface, ShopcartAdapter.GroupEdtorListener { @BindView(R.id.back) ImageView back; @BindView(R.id.title) TextView title; @BindView(R.id.subtitle) TextView subtitle; @BindView(R.id.top_bar) LinearLayout topBar; @BindView(R.id.exListView) ExpandableListView exListView; @BindView(R.id.tv_total_price) TextView tvTotalPrice; @BindView(R.id.all_chekbox) CheckBox allChekbox; @BindView(R.id.tv_delete) TextView tvDelete; @BindView(R.id.tv_go_to_pay) TextView tvGoToPay; @BindView(R.id.ll_shar) LinearLayout llShar; @BindView(R.id.ll_info) LinearLayout llInfo; @BindView(R.id.tv_share) TextView tvShare; @BindView(R.id.tv_save) TextView tvSave; @BindView(R.id.ll_cart) LinearLayout llCart; @BindView(R.id.layout_cart_empty) LinearLayout cart_empty; private Context context; private double totalPrice = 0.00;// 购买的商品总价 private int totalCount = 0;// 购买的商品总数量 private ShopcartAdapter selva; private List<StoreInfo> groups = new ArrayList<StoreInfo>();// 组元素数据列表 private Map<String, List<GoodsInfo>> children = new HashMap<String, List<GoodsInfo>>();// 子元素数据列表 private int flag = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); context = this; initDatas(); ButterKnife.bind(this); initEvents(); } private void initEvents() { selva = new ShopcartAdapter(groups, children, this); selva.setCheckInterface(this);// 关键步骤1,设置复选框接口 selva.setModifyCountInterface(this);// 关键步骤2,设置数量增减接口 selva.setmListener(this); exListView.setAdapter(selva); for (int i = 0; i < selva.getGroupCount(); i ) { exListView.expandGroup(i);// 关键步骤3,初始化时,将ExpandableListView以展开的方式呈现 } } @Override protected void onResume() { super.onResume(); setCartNum(); } /** * 设置购物车产品数量 */ private void setCartNum() { int count = 0; for (int i = 0; i < groups.size(); i ) { groups.get(i).setChoosed(allChekbox.isChecked()); StoreInfo group = groups.get(i); List<GoodsInfo> childs = children.get(group.getId()); for (GoodsInfo goodsInfo : childs) { count = 1; } } //购物车已清空 if(count==0){ clearCart(); } else{ title.setText("购物车" "(" count ")"); } } private void clearCart() { title.setText("购物车" "(" 0 ")"); subtitle.setVisibility(View.GONE); llCart.setVisibility(View.GONE); cart_empty.setVisibility(View.VISIBLE); } /** * 模拟数据<br> * 遵循适配器的数据列表填充原则,组元素被放在一个List中,对应的组元素下辖的子元素被放在Map中,<br> * 其键是组元素的Id(通常是一个唯一指定组元素身份的值) */ private void initDatas() { for (int i = 0; i < 3; i ) { groups.add(new StoreInfo(i "", "天猫店铺" (i 1) "号店")); List<GoodsInfo> products = new ArrayList<GoodsInfo>(); for (int j = 0; j <= i; j ) { int[] img = {R.drawable.goods1, R.drawable.goods2, R.drawable.goods3, R.drawable.goods4, R.drawable.goods5, R.drawable.goods6}; products.add(new GoodsInfo(j "", "商品", groups.get(i) .getName() "的第" (j 1) "个商品", 12.00 new Random().nextInt(23), new Random().nextInt(5) 1, "豪华", "1", img[i * j], 6.00 new Random().nextInt(13))); } children.put(groups.get(i).getId(), products);// 将组元素的一个唯一值,这里取Id,作为子元素List的Key } } /** * 删除操作<br> * 1.不要边遍历边删除,容易出现数组越界的情况<br> * 2.现将要删除的对象放进相应的列表容器中,待遍历完后,以removeAll的方式进行删除 */ protected void doDelete() { List<StoreInfo> toBeDeleteGroups = new ArrayList<StoreInfo>();// 待删除的组元素列表 for (int i = 0; i < groups.size(); i ) { StoreInfo group = groups.get(i); if (group.isChoosed()) { toBeDeleteGroups.add(group); } List<GoodsInfo> toBeDeleteProducts = new ArrayList<GoodsInfo>();// 待删除的子元素列表 List<GoodsInfo> childs = children.get(group.getId()); for (int j = 0; j < childs.size(); j ) { if (childs.get(j).isChoosed()) { toBeDeleteProducts.add(childs.get(j)); } } childs.removeAll(toBeDeleteProducts); } groups.removeAll(toBeDeleteGroups); //记得重新设置购物车 setCartNum(); selva.notifyDataSetChanged(); } @Override public void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked) { GoodsInfo product = (GoodsInfo) selva.getChild(groupPosition, childPosition); int currentCount = product.getCount(); currentCount ; product.setCount(currentCount); ((TextView) showCountView).setText(currentCount ""); selva.notifyDataSetChanged(); calculate(); } @Override public void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked) { GoodsInfo product = (GoodsInfo) selva.getChild(groupPosition, childPosition); int currentCount = product.getCount(); if (currentCount == 1) return; currentCount--; product.setCount(currentCount); ((TextView) showCountView).setText(currentCount ""); selva.notifyDataSetChanged(); calculate(); } @Override public void childDelete(int groupPosition, int childPosition) { children.get(groups.get(groupPosition).getId()).remove(childPosition); StoreInfo group = groups.get(groupPosition); List<GoodsInfo> childs = children.get(group.getId()); if (childs.size() == 0) { groups.remove(groupPosition); } selva.notifyDataSetChanged(); // handler.sendEmptyMessage(0); calculate(); } @Override public void checkGroup(int groupPosition, boolean isChecked) { StoreInfo group = groups.get(groupPosition); List<GoodsInfo> childs = children.get(group.getId()); for (int i = 0; i < childs.size(); i ) { childs.get(i).setChoosed(isChecked); } if (isAllCheck()) allChekbox.setChecked(true); else allChekbox.setChecked(false); selva.notifyDataSetChanged(); calculate(); } @Override public void checkChild(int groupPosition, int childPosiTion, boolean isChecked) { boolean allChildSameState = true;// 判断改组下面的所有子元素是否是同一种状态 StoreInfo group = groups.get(groupPosition); List<GoodsInfo> childs = children.get(group.getId()); for (int i = 0; i < childs.size(); i ) { // 不全选中 if (childs.get(i).isChoosed() != isChecked) { allChildSameState = false; break; } } //获取店铺选中商品的总金额 if (allChildSameState) { group.setChoosed(isChecked);// 如果所有子元素状态相同,那么对应的组元素被设为这种统一状态 } else { group.setChoosed(false);// 否则,组元素一律设置为未选中状态 } if (isAllCheck()) { allChekbox.setChecked(true);// 全选 } else { allChekbox.setChecked(false);// 反选 } selva.notifyDataSetChanged(); calculate(); } private boolean isAllCheck() { for (StoreInfo group : groups) { if (!group.isChoosed()) return false; } return true; } /** * 全选与反选 */ private void doCheckAll() { for (int i = 0; i < groups.size(); i ) { groups.get(i).setChoosed(allChekbox.isChecked()); StoreInfo group = groups.get(i); List<GoodsInfo> childs = children.get(group.getId()); for (int j = 0; j < childs.size(); j ) { childs.get(j).setChoosed(allChekbox.isChecked()); } } selva.notifyDataSetChanged(); calculate(); } /** * 统计操作<br> * 1.先清空全局计数器<br> * 2.遍历所有子元素,只要是被选中状态的,就进行相关的计算操作<br> * 3.给底部的textView进行数据填充 */ private void calculate() { totalCount = 0; totalPrice = 0.00; for (int i = 0; i < groups.size(); i ) { StoreInfo group = groups.get(i); List<GoodsInfo> childs = children.get(group.getId()); for (int j = 0; j < childs.size(); j ) { GoodsInfo product = childs.get(j); if (product.isChoosed()) { totalCount ; totalPrice = product.getPrice() * product.getCount(); } } } tvTotalPrice.setText("¥" totalPrice); tvGoToPay.setText("去支付(" totalCount ")"); //计算购物车的金额为0时候清空购物车的视图 if(totalCount==0){ setCartNum(); } else{ title.setText("购物车" "(" totalCount ")"); } } @OnClick({R.id.all_chekbox, R.id.tv_delete, R.id.tv_go_to_pay, R.id.subtitle, R.id.tv_save, R.id.tv_share}) public void onClick(View view) { AlertDialog alert; switch (view.getId()) { case R.id.all_chekbox: doCheckAll(); break; case R.id.tv_delete: if (totalCount == 0) { Toast.makeText(context, "请选择要移除的商品", Toast.LENGTH_LONG).show(); return; } alert = new AlertDialog.Builder(context).create(); alert.setTitle("操作提示"); alert.setMessage("您确定要将这些商品从购物车中移除吗?"); alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { return; } }); alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { doDelete(); } }); alert.show(); break; case R.id.tv_go_to_pay: if (totalCount == 0) { Toast.makeText(context, "请选择要支付的商品", Toast.LENGTH_LONG).show(); return; } alert = new AlertDialog.Builder(context).create(); alert.setTitle("操作提示"); alert.setMessage("总计:\n" totalCount "种商品\n" totalPrice "元"); alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { return; } }); alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { return; } }); alert.show(); break; case R.id.subtitle: if (flag == 0) { llInfo.setVisibility(View.GONE); tvGoToPay.setVisibility(View.GONE); llShar.setVisibility(View.VISIBLE); subtitle.setText("完成"); } else if (flag == 1) { llInfo.setVisibility(View.VISIBLE); tvGoToPay.setVisibility(View.VISIBLE); llShar.setVisibility(View.GONE); subtitle.setText("编辑"); } flag = (flag 1) % 2;//其余得到循环执行上面2个不同的功能 break; case R.id.tv_share: if (totalCount == 0) { Toast.makeText(context, "请选择要分享的商品", Toast.LENGTH_LONG).show(); return; } Toast.makeText(ShopcartActivity.this, "分享成功", Toast.LENGTH_SHORT).show(); break; case R.id.tv_save: if (totalCount == 0) { Toast.makeText(context, "请选择要保存的商品", Toast.LENGTH_LONG).show(); return; } Toast.makeText(ShopcartActivity.this, "保存成功", Toast.LENGTH_SHORT).show(); break; } } @Override public void groupEdit(int groupPosition) { groups.get(groupPosition).setIsEdtor(true); selva.notifyDataSetChanged(); }// Handler handler = new Handler() {// @Override// public void handleMessage(Message msg) {// super.handleMessage(msg);// //删除购物车后动态改变数量// setCartNum();// }// }; @Override protected void onDestroy() { super.onDestroy(); selva=null; groups.clear(); totalPrice=0; totalCount=0; children.clear(); }}
下载android 仿淘宝购物车 示例源码用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论